//
//  HWNewCircleViewController.swift
//  HWanGame
//
//  Created by Apple on 2024/8/13.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
import Stevia

class HWNewCircleViewController: HWBaseViewController {

    private let searchButton = UIIconButton(style: .leading(itemSpace: 0))
    ///标签
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    var httpManager = HWHomeHttpManager()
    var titles:[String] = Array()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationView.isHidden = true
        view.backgroundColor = .colorWithHexString(hexString: "#F7F8FA")
        
        view.subviews{
            searchButton
            segmentedView
        }
        
        searchButton.style { v in
            v.iconSize = CGSize(width: 16*widthScale, height: 16*widthScale)
            v.setImage("homeSearchLogo".toImage, for: .normal)
            v.setTitle("请输入关键进行搜索", for: .normal)
            v.setTitleColor("#AEB0B3".toColor, for: .normal)
            v.contentEdgeInsets = UIEdgeInsets(top: 7, left: 6, bottom: 0, right: 0)
            v.titleLabel?.font = 12.font_medium
            v.contentHorizontalAlignment = .leading
            v.addTarget(self, action: #selector(searchButtonClick), for: .touchUpInside)
            v.backgroundColor = "#EDEEF0".toColor
            v.layer.cornerRadius = 15*widthScale
            v.layer.masksToBounds = true
        }
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["圈子","论坛"]
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#444444")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleNormalFont = .systemFont(ofSize: 16*widthScale, weight: .regular)
        segmentedDataSource.titleSelectedFont = .systemFont(ofSize: 16*widthScale, weight: .semibold)
        segmentedDataSource.itemSpacing = 11*widthScale
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorImageView()
        indicator.image = "segement_dian".toImage
        indicator.indicatorWidth = 13*widthScale
        indicator.indicatorHeight = 13*widthScale
//        indicator.verticalOffset = 10*widthScale
        segmentedView.indicators = [indicator]
        
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        initFrame()
        loadData()
        
        NotificationCenter.default.addObserver(self, selector: #selector(toLuntanListVC), name: .toLuntanListVC, object: nil)
    }
    
    private func initFrame() {
        
        segmentedView.leading(0).top(CGFloat.statusHeight).height(44*widthScale).width(100*widthScale)
        searchButton.trailing(12*widthScale).top(.statusHeight+8*widthScale).width(200*widthScale).height(30*widthScale)
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
        
    }

    @objc func searchButtonClick() {
        let searchController = HWSearchController()
        searchController.modalPresentationStyle = .overFullScreen
        self.present(searchController, animated: true, completion: nil)
    }
    
    @objc private func toLuntanListVC() {
        self.segmentedView.selectItemAt(index: 1)
    }
}

//MARK: JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate
extension HWNewCircleViewController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        
        if index == 0 {
            return HWNewCircleListViewController()
        }else{
            return HWStrategyListViewController()
        }
        
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
}

//MARK: data
extension HWNewCircleViewController {
    
    func loadData(){
        
        self.segmentedDataSource.titles = ["圈子","论坛"]
        self.segmentedView.reloadData()
    }
}
